[#import "/framework/JGP.htm"  as JGP]
<!--
项目   jgp-cloud-parent
作者   LOUFEI [Gavin]
时间   2018/12/3
-->
[@JGP.head "Title"]
    <style>
        .not-published .jgp-btn{
            border-color: #f04638!important;
            background: -webkit-gradient(linear, left top, left bottom, from(rgba(237, 16, 31, 0.25)), to(#f02737))!important;
            background: linear-gradient(rgba(237, 16, 31, 0.25)), to(#f02737))!important;
        }
        .refresh-published .jgp-btn{
            border-color: #f0881d !important;
            background: -webkit-gradient(linear, left top, left bottom, from(rgba(237, 16, 31, 0.25)), to(#f0881d))!important;
            background: linear-gradient(rgba(237, 16, 31, 0.25)), to(#f0881d))!important;
        }
    </style>
[/@JGP.head]
[@JGP.body]
    <jgp-div _row="['30','*']" _runit="px">
        <div slot="1-1">
            <jgp-btn _fn="createModel">创建流程模板</jgp-btn>
        </div>
        <jgp-grid slot="2-1" _id="modelGrid" _url="/workflow/editorApiController/queryAllModels"
                  _record_tools="[{'icon':'edit','tip':'修改','fn':'editModelFn'},
                  {'icon':'edit','tip':'设计','fn':'designFlowFn'},
                  {'icon':'trash-o','tip':'删除','fn':'removeFn'}]">
            <jgp-grid-col _name="createTime" _transform_value="transformTimeFn"></jgp-grid-col>
            <jgp-grid-col _name="lastUpdateTime" _transform_value="transformTimeFn"></jgp-grid-col>
            <jgp-grid-col _name="deploymentId" _transform_value="getDeployStatusFn"
                          _filter_render="filterRenderFn"
                          _fns="['publishFlow']"
                          _wrap="<button>[content]</button>"></jgp-grid-col>
        </jgp-grid>
    </jgp-div>
[/@JGP.body]

[@JGP.foot]
    <script>
        function createModel() {
            var config = {
                url: '/workflow/editorController/addModel',
                callback: function (result) {
                    if (result) {
                        location.reload();
                    }
                }
            };
            JGP.$dialog(config);
        }

        function editModelFn(param) {
            var config = {
                url: '/workflow/editorController/editModel/'+param.back.record.id,
                callback: function (result) {
                    if (result) {
                        location.reload();
                    }
                }
            };
            JGP.$dialog(config);
        }


        function removeFn(param) {
            JGP.$confirm("是否删除:" + param.back.name, function () {
                JGP.$post('/workflow/editorApiController/removeModel', {modelId: param.back.record.id}, function (result) {
                    if (result.flag) {
                        location.reload();
                    }
                })
            })

        }

        function designFlowFn(param) {
            var config = {
                title: '设计流程:' + param.back.record.name,
                url: '/workflow/editorController/enter/' + param.back.record.id,
                callback: function (result) {
                    if (result) {
                        location.reload();
                    }
                }
            };
            JGP.$dialog(config);
        }

        function transformTimeFn(data) {
            var time = JGP.Date.format(new Date(Number(data.back.value)), 'YYYY-MM-DD hh:mm:ss');
            return time;
        }

        function getDeployStatusFn(data) {
            var record = data.back.record;

            if(record.deploymentId){
                if(record.deploymentTime<record.lastUpdateTime){
                    return '更新发布';
                }else{
                    return '已发布';
                }
            }else{
                return '未发布'
            }
        }

        function filterRenderFn(data){
            var record = data.back.record;
            return (record.deploymentTime<record.lastUpdateTime)?'refresh-published':data.back.value?'published':'not-published';
        }

        function publishFlow(data){
            debugger
            var record = data.back.record;
            if(!data.back.value||record.deploymentTime<record.lastUpdateTime){
                JGP.$confirm("是否要发布:"+data.back.record.name,function(){
                    JGP.$post('/workflow/processApiController/publish',{modelId:data.back.record.id},function (result) {
                        location.reload();
                    },function(){
                        console.error('error');
                    })
                })
            }
        }

    </script>
[/@JGP.foot]